<?php
/**
 * Created by PhpStorm.
 * User: sunlight
 * Date: 2018/11/23
 * Time: 15:19
 */

namespace app\api\controller\V1;

use app\api\controller\Common;
use app\common\lib\exception\ApiException;
use think\Exception;
use think\Log;

class Index extends Common{

    /**
     * 获取首页接口
     */
    public function index(){
        $heads=model('Product')->getIndexHeadNormalProduct();
        $heads=$this->getDealProduct($heads);

        $positions=model('Product')->getIndexHeadNormalProduct();
        $positions=$this->getDealProduct($heads);

        $store=model('Store')->getIndexHeadNormalStore();
        $store=$this->getDealProduct($store);

        $result = [
            'heads' => $heads,
            'positions' => $positions,
            'store' => $store
        ];

        return show(config('code.success'),'ok',$result,200);
    }

    /**
     * 客户端初始化接口
     *      检测app是否许需要升级
     * @return \think\response\Json
     */
    public function init(){
        // app_type 去ent_version 查询
        $version=model('Version')->getLastNormalVersionByAppType($this->headers['app_type']);

        if(empty($version)){
            return new ApiException('error',404);
        }

        if ($version->version > $this->headers['version']){
            $version -> is_update = $version -> is_force == 1 ? 2 : 1;
            //$version -> is_update = 1;
        }else{
            // 0 不更新 ， 1需要更新, 2强制更新
            $version -> is_update = 0;
        }

        //记录用户的基本信息用于统计
        $actives=[
            'version'=>$this->headers['version'],
            'app_type'=>$this->headers['app_type'],
            'did'=>$this->headers['did'],
        ];

        try{
            model('AppActive')->add($actives);
        }catch (Exception $e){/*Log::write($e);*/}//不需要打印异常信息

        return show(config('code.success'), 'OK', $version, 200);
    }

}